package com.shimao.celnet.payment.service.impl;

import com.github.pagehelper.PageHelper;
import com.shimao.celnet.payment.form.IInterfaceLogForm;
import com.shimao.celnet.payment.model.IInterfaceLog;
import com.shimao.celnet.payment.service.IInterfaceLogService;
import com.shimao.celnet.payment.util.FinalUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;


/**
 * 接口日志 管理
 * Created by ensure on 2017/12/3.
 */
@Service
public class IInterfaceLogServiceImpl extends BaseServiceImpl<IInterfaceLog> implements IInterfaceLogService {
    protected Logger logger = Logger.getLogger(this.getClass());



    /**
     * 分页查询
     * @param formBean
     * @param pageNum
     * @param length
     * @return
     */
    @Override
    public IInterfaceLogForm getByPage(IInterfaceLogForm formBean, int pageNum, int length) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(IInterfaceLog.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(formBean.getStartDate()) && StringUtils.isNotEmpty(formBean.getEndDate())) {
            //应用系统注册GUID应用系统注册
            criteria.andBetween("startDate",  formBean.getStartDate(),  formBean.getEndDate() );
        }
        if (StringUtil.isNotEmpty(formBean.getInterfaceName())) {
            //接口名称
            criteria.andLike("interfaceName", "%" + formBean.getInterfaceName() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getInterfaceType())) {
            //类型callIn，callOut
            criteria.andLike("interfaceType", "%" + formBean.getInterfaceType() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getAppName())) {
            //应用系统
            criteria.andLike("appName", "%" + formBean.getAppName() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getIsProcess())) {
            //人工处理0
            criteria.andLike("isProcess", "%" + formBean.getIsProcess() + "%");
        }
        if (StringUtil.isNotEmpty(formBean.getRequestText())) {
            //人工处理0
            criteria.andLike("requestText", "%" + formBean.getRequestText() + "%");
        }

        //分页查询
        PageHelper.startPage(pageNum, length);
        List<IInterfaceLog> tableList = getByExample(example);
        formBean.setPageInfo(tableList);
        return  formBean;
    }

    /**
     *新增保存
     * @return
     */
    @Override
    public int save(IInterfaceLog sBean){
        int retCnt=0;
        sBean.setGuid(getGuid());//guid
        retCnt = super.save(sBean);

        return retCnt;
    }

    /**
     *修改保存
     * @return
     */
    @Override
    public int update(IInterfaceLog sBean){
        String loginUserGuid = FinalUtil.INTER_FACE_USER_ID;
        sBean.setUpdateUserGuid(loginUserGuid);
        int retCnt = super.update(sBean);

        return retCnt;
    }
    @Override
    public List<IInterfaceLog>  getInfoForPushAgain(IInterfaceLogForm formBean) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(IInterfaceLog.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(formBean.getStartDate()) && StringUtils.isNotEmpty(formBean.getEndDate())) {
            //应用系统注册GUID应用系统注册
            criteria.andBetween("startDate",  formBean.getStartDate(),  formBean.getEndDate() );
        }
        if (StringUtil.isNotEmpty(formBean.getInterfaceName())) {
            //接口名称
            criteria.andLike("interfaceName", "%" + formBean.getInterfaceName() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getInterfaceType())) {
            //类型callIn，callOut
            criteria.andLike("interfaceType", "%" + formBean.getInterfaceType() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getAppName())) {
            //应用系统
            criteria.andLike("appName", "%" + formBean.getAppName() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getIsProcess())) {
            //人工处理0
            criteria.andLike("isProcess", "%" + formBean.getIsProcess() + "%");
        }
        if (StringUtil.isNotEmpty(formBean.getRequestText())) {
            //人工处理0
            criteria.andLike("requestText", "%" + formBean.getRequestText() + "%");
        }
        List<IInterfaceLog> tableList = getByExample(example);
        return  tableList;
    }
}
